using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Data.Common;

namespace Destra.Tools.DACGenerator
{
    public partial class PreviewSqlDialog : Form
    {
        private string m_connectionString;
        private string m_sql;

        public PreviewSqlDialog(string connectionString, string sql)
        {
            InitializeComponent();

            m_connectionString = connectionString;
            m_sql = sql;
        }

        private string GetServer()
        {
            string server = null;

            Regex regex = new Regex(@"Data Source=(?<server>[^;]*)", RegexOptions.IgnoreCase);
            Match match = regex.Match(m_connectionString);
            if (match.Success == true)
            {
                server = match.Groups["server"].Value;
            }

            return server;
        }

        private void PreviewSqlDialog_Load(object sender, EventArgs e)
        {
            List<QueryInfo.Parameter> parameters;
            List<QueryInfo.OutputColumn> outputColumns;
            QueryInfo.QueryTables queryTables;

            QueryInfo queryPlan = new QueryInfo(m_connectionString, m_sql);
            queryPlan.Load(out parameters, out outputColumns, out queryTables);

            foreach (QueryInfo.Parameter parameter in parameters)
            {
                ParametersGridView.Rows.Add(new string[] { parameter.Name, parameter.SqlDbType.ToString(), "" });

            }
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            Application.DoEvents();

            try
            {
                using (SqlConnection connection = new SqlConnection(m_connectionString))
                {
                    using (SqlCommand command = new SqlCommand(m_sql))
                    {
                        command.Connection = connection;
                        command.Connection.Open();
                        command.CommandType = CommandType.Text;

                        foreach (DataGridViewRow row in ParametersGridView.Rows)
                        {
                            command.Parameters.Add(new SqlParameter(row.Cells[0].Value.ToString(), row.Cells[2].Value.ToString()));

                        }

                        DataSet ds = new DataSet();
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            adapter.Fill(ds);
                            ResultsGridView.Columns.Clear();
                            foreach (DataColumn col in ds.Tables[0].Columns)
                            {
                                ResultsGridView.Columns.Add(col.ColumnName + Guid.NewGuid().ToString(), col.ColumnName);
                            }

                            int rowCount = 0;
                            ResultsGridView.Rows.Clear();
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                List<string> values = new List<string>();
                                foreach (DataColumn col in ds.Tables[0].Columns)
                                {
                                    values.Add(row[col].ToString());
                                }

                                ResultsGridView.Rows.Add(values.ToArray());

                                rowCount += 1;
                                if (rowCount.Equals(100))
                                {
                                    MessageBox.Show("Only the Top 100 Records have been shown");
                                    break;
                                }
                            }



                        }
                    }
                }
                UseWaitCursor = false;
                Application.DoEvents();

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            UseWaitCursor = false;
            Application.DoEvents();

        }


    }
}